﻿/**
 * DataDrivenAddIn.cs
 *
 * Copyright (C) 2008,  iron9light
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections;
//using System.Collections.Generic;
using System.Reflection;
using NUnit.Core;
using NUnit.Core.Extensibility;

namespace NUnitExtension.DataDriven.AddIn
{
    [NUnitAddin(Name = "Data Driven Extension", Description = "Run test cases with user-defined data source.")]
    public class DataDrivenAddIn : IAddin, ITestCaseBuilder
    {
        private readonly DataDrivenFactory m_DataDrivenFactory = new DataDrivenFactory();

        public bool Install(IExtensionHost host)
        {
            if (host == null)
                throw new ArgumentNullException("host");

            IExtensionPoint testCaseBuilders = host.GetExtensionPoint("TestCaseBuilders");
            if (testCaseBuilders == null)
                return false;

            testCaseBuilders.Install(this);
            return true;
        }

        public bool CanBuildFrom(MethodInfo method, Test suite)
        {
            return DataDrivenFramework.IsDataDriven(method);
        }

        public Test BuildFrom(MethodInfo method, Test suite)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            DataDrivenSuite dataDrivenSuite = m_DataDrivenFactory.CreateSuite(method);
            Attribute[] dataFactorys = DataDrivenFramework.GetDataFactoryAttributes(method);

            foreach (Attribute dataFactory in dataFactorys)
            {
                //IList<DataDrivenTestCase> testCases = m_DataDrivenFactory.GreateTestCases(method, dataFactory);
                IList testCases = m_DataDrivenFactory.GreateTestCases(method, dataFactory);
                foreach (DataDrivenTestCase testCase in testCases)
                {
                    dataDrivenSuite.Add(testCase);
                }
            }

            return dataDrivenSuite;
        }
    }
}
